﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KhatVongViet.Models;
using KhatVongViet.Controllers;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.Objects;
using PagedList;

namespace KhatVongViet.Controllers
{
    public class ConfessionController : Controller
    {
        KhatVongVietEntities dataContext = new KhatVongVietEntities();
        GetData getData = new GetData();

        [HttpGet]
        public ActionResult Detail(int idConfession)
        {
            Article camxucviet = getData.GetCamXucViet(idConfession);
            return View(camxucviet);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection form)
        {
            string sTieuDe = form["txtTieuDe"];
            string sEmail = form["txtEmail"];
            string sNoiDung = form["txtNoiDung"];
            Article cxv = new Article()
            {
                TypeID = "CamXuc",
                Title = sTieuDe,
                Email = sEmail,
                Context = sNoiDung,
                Publish = false,
                DateCreate = DateTime.Now
            };

            dataContext.Articles.AddObject(cxv);
            dataContext.SaveChanges();
            return RedirectToAction("Default", "Home");
        }

        //public ActionResult Edit()
        //{
        //    int id = Convert.ToInt32(Request["id"]);
        //    var tempcxv = dataContext.Articles.First(m => m.ID == id);
        //    return View(tempcxv);
        //}

        //[HttpPost]
        //public ActionResult Edit(FormCollection form)
        //{
        //    int id = Convert.ToInt32(Request["id"]);
        //    var tempcxv = dataContext.Articles.First(m => m.ID == id);
        //    string sTieuDe = form["TieuDe"];
        //    string sEmail = form["Email"];
        //    string sNoiDung = form["NoiDung"];

        //    tempcxv.Title = sTieuDe;
        //    tempcxv.Email = sEmail;
        //    tempcxv.Context = sNoiDung;

        //    UpdateModel(tempcxv);

        //    dataContext.SaveChanges();
        //    return RedirectToAction("ManageConfession", "Administrator");
        //}

        //public ActionResult AllDetail(int page = 1)
        //{
        //    List<Article> listAllCamXucViet = getConfession.GetAllCamXucViet();
        //    int numberitemperpage = 3;
        //    string chuoi = "";
        //    string temp = "";
        //    chuoi+="<div class=\"div-title\">";
        //        chuoi+="<div class=\"div-title-text\">";
        //            chuoi+="Chi tiết confession / Trang " + page;
        //        chuoi+="</div>";
        //    chuoi+="</div>";
        //    chuoi+="<div class=\"div-title-end\">";
        //        chuoi+="<div class=\"div-title-end-peace\">";
        //            chuoi+="<img src=\"../../Content/images/top-title-image.png\" />";
        //        chuoi+="</div>";
        //        chuoi+="<div class=\"div-title-end-peace\">";
        //            chuoi+="<img src=\"../../Content/images/bot-title-image.png\" />";
        //        chuoi+="</div>";
        //    chuoi+="</div>";
        //    chuoi += "<br /><br /><br />";

        //    for (int i = numberitemperpage * page - numberitemperpage; i < listAllCamXucViet.Count && i < numberitemperpage * page; i++)
        //    {

        //        chuoi+="<div class=\"box-confession-alldetail-peaces\">";
        //            chuoi+="<div class=\"box-image-cxv\">";
        //                chuoi+="<img src = \""+listAllCamXucViet[i].Image+"\" />";
        //            chuoi+="</div>";
        //            chuoi+="<div class=\"box-camxucviet-title\">";
        //                if (listAllCamXucViet[i].Title.Length <= 50)
        //                {
        //                    temp = listAllCamXucViet[i].Title;
        //                }
        //                else
        //                {
        //                    temp = listAllCamXucViet[i].Title.Substring(0, listAllCamXucViet[i].Title.Substring(0, 50).LastIndexOf(" ")) + "...";
        //                }
        //                chuoi += "<a class=\"titlelink\" href=\"/Confession/Detail?idConfession=" + listAllCamXucViet[i].ID + "\">" + temp + "</a>";
        //            chuoi+="</div>";
        //            chuoi+="<div class=\"box-camxucviet-email\">";
        //                if (listAllCamXucViet[i].Email != null)
        //                {
        //                    chuoi+="Chủ confession : "+listAllCamXucViet[i].Email;
        //                }
        //                else
        //                {
        //                    chuoi+="Chủ confession : Xin được dấu tên";
        //                }
        //            chuoi+="</div>";
        //            chuoi+="<div class=\"box-camxucviet-detail\">";
        //            chuoi+="\" ";
        //                if (listAllCamXucViet[i].Context.Length <= 300)
        //                {
        //                    chuoi+=listAllCamXucViet[i].Context;
        //                }
        //                else
        //                {
        //                    chuoi += listAllCamXucViet[i].Context.Substring(0, listAllCamXucViet[i].Context.Substring(0, 300).LastIndexOf(" ")) + "...";
        //                }
        //            chuoi += " \"";
        //            chuoi+="</div>";
        //        chuoi+="</div>";
        //    }
        //    ViewBag.AllCamXucViet = chuoi;
        //    return View(dataContext.Articles.Where(p => p.TypeID == "CamXuc").OrderBy(v => v.ID).ToPagedList(page, numberitemperpage));
        //}

    }
}
